﻿using System.Linq;
using Missbot.Common;
using Missbot.Core.Base;
using Dolany.UtilityTool;
using JetBrains.Annotations;
using Missbot.AIModule.Base;
using Missbot.AIModule.OnlineStore;

namespace Missbot.AIModule.Vip
{
    /// <summary>
    /// VIP服务模块
    /// </summary>
    [UsedImplicitly]
    public class VipServiceMod : IAIModule
    {
        public override string Name => "VIP服务";

        #region Svcs

        public DailyVipShopSvc DailyVipShopSvc { get; set; }
        public WaiterSvc WaiterSvc { get; set; }

        #endregion

        [ModuleEnter(Command = "vip商店 钻石商店", Description = "打开vip商店(每日刷新7个服务项目)")]
        public bool VipShop(InboxSession session)
        {
            var osPerson = OSPersonDA.GetPerson(session.User.ID);
            if (osPerson.Diamonds <= 0)
            {
                session.SendBack("很抱歉，你当前的钻石余额不足，无法打开vip商店！");
                return false;
            }

            var goodsName = DailyVipGoodsDA.GetToday(session.User.ID).GoodsName;
            var goods = goodsName.Select(g => DailyVipShopSvc[g]).ToList();
            var goodsMsg = string.Join("\n", goods.Select(g => $"{g.Name}({g.DiamondsNeed.CurencyFormat("Diamond")}):{g.Description}"));
            var msg = $"今天提供的vip服务有：\n{goodsMsg}\n你当前余额为：{osPerson.Diamonds.CurencyFormat("Diamond")}";

            session.SendBack(msg);
            return true;
        }

        [ModuleEnter(Command = "刷新vip商店 刷新钻石商店", Description = "刷新vip商店（花费10钻石）")]
        public bool RefreshVipShop(InboxSession session)
        {
            var osPerson = OSPersonDA.GetPerson(session.User.ID);
            if (osPerson.Diamonds < 10)
            {
                session.SendBack($"很抱歉，你当前的钻石余额不足，无法刷新vip商店！({osPerson.Diamonds}/10)");
                return false;
            }

            if (!WaiterSvc.WaitForConfirm(session, $"此操作将花费{10.CurencyFormat("Diamond")}，是否继续？"))
            {
                session.SendBack("操作取消！");
                return false;
            }

            osPerson.Diamonds -= 10;
            osPerson.Update();

            DailyVipGoodsDA.Refresh(session.User.ID);

            session.SendBack("刷新成功！");
            return true;
        }

        [ModuleEnter(Command = "查看装备", Description = "查看指定名称的装备", SyntaxHint = "[装备名]")]
        public bool ViewArmer(InboxSession session, string name)
        {
            var armer = DailyVipShopSvc[name];
            if (armer == null)
            {
                session.SendBack("未查找到相关装备！");
                return false;
            }

            var msg = $"{armer.Name}\n    {armer.Description}\n售价：{armer.DiamondsNeed.CurencyFormat("Diamond")}";
            session.SendBack(msg);
            return true;
        }

        [ModuleEnter(Command = "我的装备", Description = "查看自己拥有的装备")]
        public bool MyArmer(InboxSession session)
        {
            var record = VipArmerDA.Get(session.User.ID);
            if (record == null || record.Armers.IsNullOrEmpty())
            {
                session.SendBack("你尚未持有任何装备！");
                return false;
            }

            var armerMsgs = record.Armers.Select(r => $"{r.Name}：{r.Description}{(r.ExpiryTime.HasValue ? $"({r.ExpiryTime})" : string.Empty)}");
            var msg = $"你当前持有的装备有：\n{string.Join("\n", armerMsgs)}";
            session.SendBack(msg);
            return true;
        }
    }
}